/********************************************************************************
* @author: Ternura_xiang
* @email: 2022101883@nit.edu.cn
* @date: Apr 5, 2025
* @version: 1.0
* @description: 编码器驱动及事件
********************************************************************************/
#include "encoder.h"
#include "tim.h"
#include "display.h"

#define ENCODER1_TIM htim3
#define ENCODER2_TIM hrim2

int16_t encoder1_data, encoder2_data;


/**
 * @brief 编码器初始化
 */
void Encoder_Init(void){
    HAL_TIM_Encoder_Start(&htim2, TIM_CHANNEL_ALL); //开启编码器模式
    HAL_TIM_Encoder_Start(&htim3, TIM_CHANNEL_ALL);
    HAL_TIM_Base_Start_IT(&htim2);                  //开启编码器的中断
    HAL_TIM_Base_Start_IT(&htim3);
}

// TODO: 将Encoder_Process()拆为多个模块
/**
 * @brief 编码器事件
 */
void Encoder_Process(void) {
    // 主界面
    if (display_mode == DISPLAY_MODE_MAIN) {
        if(encoder1_data != 0){
            int8_t step = (encoder1_data > 0) ? 1 : -1;
            display_mode_select_now += step;
            // 限制模式范围
            if (display_mode_select_now > DISPLAY_MODE_SETTING) 
                display_mode_select_now = DISPLAY_MODE_SETTING;
            if (display_mode_select_now < DISPLAY_MODE_THRESHOLD) 
                display_mode_select_now = DISPLAY_MODE_THRESHOLD;
        }
    }
    // PWM模式
    else if (display_mode == DISPLAY_MODE_PWM) {
        // 选择需要调整的参数(同上主界面)
        if(pwm_change == 0){
            if(encoder1_data != 0){
                int8_t step = (encoder1_data > 0) ? 1 : -1;
                pwm_select_now += step;
                if (pwm_select_now > PWM_SELECT_DUTY2) 
                    pwm_select_now = PWM_SELECT_DUTY2;
                if (pwm_select_now < PWM_SELECT_FREQ) 
                    pwm_select_now = PWM_SELECT_FREQ;
            }
        }
        // 设置参数
        else{
            // 设置频率
            if (pwm_select_now == PWM_SELECT_FREQ) {
                if(encoder1_data != 0){
                    int8_t step = (encoder1_data > 0) ? -1 : 1;
                    TIM1->ARR += step;
                    pwm_freq_change_flag = 1;
                }
            }
            // 设置PWM1模式
            else if (pwm_select_now == PWM_SELECT_PWM1) {
                if(encoder1_data != 0){
                    if(pwm1_mode) {
                        HAL_TIM_PWM_Stop(&htim1, TIM_CHANNEL_4);
                        pwm1_mode = 0;
                    }
                    else {
                        HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_4);
                        pwm1_mode = 1;
                    }
                    pwm_mode_change_flag = 1;   // PWM模式更新标志
                }
            }
            // 设置PWM1占空比
            else if (pwm_select_now == PWM_SELECT_DUTY1) {
                if(encoder1_data != 0){
                    int8_t step = (encoder1_data > 0) ? 1 : -1;
                    // 设置占空比数值
                    if(pwm_duty_change_num){
                        if((pwm_duty1%10 >= 9 && step != 1) && (pwm_duty1%10 <= 1 && step == 1)){
                            pwm_duty1 += step;}
                    }
                    else{
                        if((pwm_duty1 >= 90 && step == 1) || (pwm_duty1 < 10 && step != 1));
                        else{pwm_duty1 += step * 10;}
                    }
                    // 配置定时器
                    TIM1->CCR4 = TIM1->ARR * pwm_duty1 / 100;

                    pwm_duty_change_flag = 1;
                }
            }
            // 设置PWM2模式
            else if (pwm_select_now == PWM_SELECT_PWM2) {
                if(encoder1_data != 0){
                    pwm_mode_change_flag = 1;   // PWM模式更新标志
                    if(pwm2_mode) {
                        HAL_TIM_PWM_Stop(&htim1, TIM_CHANNEL_4);
                        pwm2_mode = 0;
                    }
                    else {
                        HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_4);
                        pwm2_mode = 1;
                    }
                }
            }
            // 设置PWM2占空比
            else if (pwm_select_now == PWM_SELECT_DUTY2) {
                if(encoder1_data != 0){
                    int8_t step = (encoder1_data > 0) ? 1 : -1;
                    // 设置占空比数值
                    if(pwm_duty_change_num){
                        if((pwm_duty2%10 >= 9 && step != 1) && (pwm_duty2%10 <= 1 && step == 1)){
                            pwm_duty2 += step;}
                    }
                    else{
                        if((pwm_duty2 >= 90 && step == 1) || (pwm_duty2 < 10 && step != 1));
                        else{pwm_duty2 += step * 10;}
                    }
                    // 配置定时器
                    TIM1->CCR1 = TIM1->ARR * pwm_duty2 / 100.0;

                    pwm_duty_change_flag = 1;
                }
            }
        }
    }
    encoder1_data = 0;  // 处理完成后清零
    encoder2_data = 0;
}

/**
 * @brief 定时器回调函数，实现编码器检测
 */
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) {
    // 读取计数方向
    uint8_t direction = __HAL_TIM_IS_TIM_COUNTING_DOWN(htim);
    if (htim == &htim2) {
        encoder2_data += direction? 1 : -1;
    }
    else if (htim == &htim3) {
        encoder1_data += direction? 1 : -1;
    }
}
